package com.cug.campus_sh_market_server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cug.campus_sh_market_server.entity.param.OrderParam;
import com.cug.campus_sh_market_server.entity.param.PageParam;
import com.cug.campus_sh_market_server.entity.param.goodstype.GoodsTypeAddParam;
import com.cug.campus_sh_market_server.entity.param.goodstype.GoodsTypeQueryParam;
import com.cug.campus_sh_market_server.entity.param.goodstype.GoodsTypeUpdateParam;
import com.cug.campus_sh_market_server.entity.param.user.UserAddParam;
import com.cug.campus_sh_market_server.entity.param.user.UserQueryParam;
import com.cug.campus_sh_market_server.entity.param.user.UserRestPasswordParam;
import com.cug.campus_sh_market_server.entity.param.user.UserUpdateParam;
import com.cug.campus_sh_market_server.entity.po.GoodsType;
import com.cug.campus_sh_market_server.entity.po.User;
import com.cug.campus_sh_market_server.entity.vo.ResultData;
import com.cug.campus_sh_market_server.entity.vo.goodstype.GoodsTypeVO;
import com.cug.campus_sh_market_server.entity.vo.user.UserVO;
import com.cug.campus_sh_market_server.mapper.UserMapper;
import com.cug.campus_sh_market_server.mapper.vomapper.UserVOMapper;
import com.cug.campus_sh_market_server.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cug.campus_sh_market_server.util.*;
import com.nycreativity.elecreativity.entity.vo.PageDataVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 用户 服务实现类
 * </p>
 *
 * @author sxt
 * @since 2022-03-22
 */
@Service
@Slf4j
public class UserServiceImpl extends CustomRestfullServiceImpl
        <User, UserQueryParam, UserAddParam, UserUpdateParam, UserVO> implements UserService, UserDetailsService {

    @Override
    public UserVO queryById(Integer id) {
        return voBaseMapper.selectById(id);
    }

    @Override
    public User queryUserByAccount(String account) {
        if (account == null) {
            return null;
        }
        // 需要保证account在数据库中唯一
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<User>()
                .eq(User::getAccount, account);
        return baseMapper.selectOne(queryWrapper);
    }

    @Override
    public UserVO queryUsersById(Integer id) {
        return voBaseMapper.selectById(id);
    }

    @Override
    public boolean resetPassword(UserRestPasswordParam passwordParam) {
        if (!checkAdminUser() && !UserContext.get().getId().equals(passwordParam.getId())) {
            return false;
        }
        User user = getById(passwordParam.getId());
        if (user != null) {
            String salt = UUIDUtils.getCode();
            user.setSalt(salt)
                    .setPassword(MD5Util.encode(passwordParam.getPassword() + salt));
            user.modify();
            return updateById(user);
        }
        return false;
    }

    @Override
    public UserVO saveWithBackData(UserAddParam userAddParam) {
        User tmpUser = MappingUtil.getMappingObject(userAddParam, entityClass);
        tmpUser.create();
        String salt = UUIDUtils.getCode();
        tmpUser.setSalt(salt)
                .setPassword(MD5Util.encode(tmpUser.getPassword() + salt));
        log.info("新增用户信息记录操作, 用户信息: {}", tmpUser);
        int rs = baseMapper.insert(tmpUser);
        if (rs > 0) {
            return voBaseMapper.selectById(getEntityId(tmpUser));
        }
        return null;
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        return queryUserByAccount(username);
    }

    @Override
    public PageDataVO<UserVO> queryDataByPage(UserQueryParam userQueryParam, PageParam pageParam, OrderParam orderParam) {
        QueryWrapper<UserVO> queryWrapper = getQueryWrapper(userQueryParam, orderParam);
        if (!checkAdminUser() && queryWrapper != null) {
            queryWrapper.lambda().eq(UserVO::getId, UserContext.get().getId());
        }
        Page<UserVO> selectPage = voBaseMapper.selectPage(new Page<>(pageParam.getCurrentPage(), pageParam.getPageSize()), queryWrapper);
        return PageUtil.convert(selectPage);
    }
}
